package com.jie.jie.Fanshe;

import android.content.Context;

import com.jaydenxiao.common.commonutils.ACache;
import com.jaydenxiao.common.commonutils.LogUtils;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2017/4/18.
 */

public class ReflectUtils {
    private static String TAG = "tag";


    public void readFromAcach(Context context) {
        Class<? extends BaseActy> aClass = (Class<? extends BaseActy>) context.getClass();
        ACache cache = ACache.get(context);
        try {
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                String type = field.getType().toString();
                if (isSava(type.toString())) {
                    if (type.equals("int") || type.contains("Integer")) {
                        if ((int)field.get(context) == 0)
                            if (cache.getAsString(aClass.getName() + field.getName()) != null) {

                                Integer integer = Integer.valueOf(cache.getAsString(aClass.getName() + field.getName()));
                                field.set(context, integer);
                            }
                        LogUtils.loge((field.get(context)).toString());
                    }
                    if (type.equals("double") || type.contains("Double")) {
                        if ((double)field.get(context) == 0.0)
                            if (cache.getAsString(aClass.getName() + field.getName()) != null) {

                                Double aDouble = Double.valueOf(cache.getAsString(aClass.getName() + field.getName()));
                                field.set(context, aDouble);
                            }
                    }
                    if (type.equals("char") || type.contains("Char")) {
                    }
                    if (type.equals("long") || type.contains("Long")) {
                        if ((long)field.get(context) == 0)
                            if (cache.getAsString(aClass.getName() + field.getName()) != null) {

                                Long aLong = Long.valueOf(cache.getAsString(aClass.getName() + field.getName()));
                                field.set(context, aLong);
                            }
                    }
                    if (type.equals("short") || type.contains("Short")) {
                        if ((short)field.get(context) == 0)
                            if (cache.getAsString(aClass.getName() + field.getName()) != null) {

                                Short aShort = Short.valueOf(Short.valueOf(cache.getAsString(aClass.getName() + field.getName())));
                                field.set(context, aShort);
                            }
                    }
                    if (type.equals("float") || type.contains("Float")) {
                        if ((float)field.get(context) == 0)
                            if (cache.getAsString(aClass.getName() + field.getName()) != null) {
                                Float aFloat = Float.valueOf(cache.getAsString(aClass.getName() + field.getName()));
                                field.set(context, aFloat);
                            }

                    }
                    if (type.equals("boolean") || type.contains("Boolean")) {
                        if ((boolean)field.get(context) == false)
                            if (cache.getAsString(aClass.getName() + field.getName()) != null) {
                                Boolean aBoolean = Boolean.valueOf(cache.getAsString(aClass.getName() + field.getName()));
                                field.set(context, aBoolean);
                            }
                    }
                    if (type.equals("byte") || type.contains("Byte")) {
                        if ((byte)field.get(context) == 0)
                            if (cache.getAsString(aClass.getName() + field.getName()) != null) {

                                Byte aByte = Byte.valueOf(cache.getAsString(aClass.getName() + field.getName()));
                                field.set(context, aByte);
                            }
                    }
                    if (type.contains("String")) {
                        if (field.get(context) == null)
                            if (cache.getAsString(aClass.getName() + field.getName()) != null) {
                                String asString = cache.getAsString(aClass.getName() + field.getName());
                                field.set(context, asString);

                            }
//                        }
                    }
                    LogUtils.loge(field.getName() + "----" + field.getType() + "----" + field.get(context));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public void sava(Context context) {
        Class<? extends BaseActy> aClass = (Class<? extends BaseActy>) context.getClass();
        ACache cache = ACache.get(context);
        try {
            BaseActy baseActy = aClass.newInstance();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                Class<?> type = field.getType();
                LogUtils.loge(isSava(type + "") + "----" + type.toString());
                if (isSava(type.toString())) {
                    cache.put(aClass.getName() + type.getName(), String.valueOf(field.get(context)));
                    LogUtils.loge(field.getName() + "----" + field.getType() + "----" + field.get(context));
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    private boolean isSava(String type) {
        if ("int".equals(type) || type.equals("class java.lang.Integer")) {
            return true;
        }
        if ("double".equals(type) || type.equals("class java.lang.Double")) {
            return true;
        }
        if ("char".equals(type) || type.equals("class java.lang.Char")) {
            return true;
        }
        if ("long".equals(type) || type.equals("class java.lang.Long")) {
            return true;
        }
        if ("short".equals(type) || type.equals("class java.lang.Short")) {
            return true;
        }
        if ("float".equals(type) || type.equals("class java.lang.Float")) {
            return true;

        }
        if ("boolean".equals(type) || type.equals("class java.lang.Boolean")) {
            return true;
        }
        if ("byte".equals(type) || type.equals("class java.lang.Byte")) {
            return true;
        }
        if ("class java.lang.String".equals(type)) {
            return true;
        }
        return false;
    }

}
